﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Edge.OAuth
{
    public class OAuth2UserData
    {
        const string FACEBOOK_ME_URL = "https://graph.facebook.com/me";
        const string YANDEX_ME_URL = "https://api-yaru.yandex.ru/me/";
        const string MAILRU_ME_URL = "http://www.appsmail.ru/platform/api";
        const string VK_ME_URL = "https://api.vkontakte.ru/method/getProfiles";
        const string LIVE_ME_URL = "https://apis.live.net/v5.0/me";
        const string ODNOKLASSNIKI_ME_URL = "http://api.odnoklassniki.ru/fb.do";

        const string OAUTH_TOKEN = "oauth_token";
        const string FB_OAUTH_TOKEN = "access_token";
        const string MAILRU_SIG = "sig";
        const string LIVE_ACCESS_TOKEN = "access_token";

        public static string GetUserData(string me_url, Dictionary<string, string> parameters)
        {
            string url = string.Format("{0}{1}", me_url, OAuthCommonUtils.GetParametersString(parameters));
            return OAuthCommonUtils.GetPlainTextResponse(url);
        }

        public static string GetYandexUserData(string access_token)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string> { { OAUTH_TOKEN, access_token } };
            return GetUserData(YANDEX_ME_URL, parameters);
        }

        public static string GetVKUserData(string access_token, Dictionary<string, string> parameters)
        {
            parameters.Add(LIVE_ACCESS_TOKEN, access_token);
            return GetUserData(VK_ME_URL, parameters);
        }

        public static string GetOdnoklassnikiUserData(string access_token, string secret_key, Dictionary<string, string> dict)
        {
            string sig = GetSig(GetMD5(access_token + secret_key), dict);
            dict.Add("access_token", access_token);
            dict.Add("sig", sig);
            return GetUserData(ODNOKLASSNIKI_ME_URL, dict);
        }

        public static string GetLiveUserData(string access_token)
        {
            return GetUserData(LIVE_ME_URL, new Dictionary<string, string>() { { LIVE_ACCESS_TOKEN, access_token } });
        }

        public static string GetFacebookUserData(string access_token)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string> { { FB_OAUTH_TOKEN, access_token } };
            return GetUserData(FACEBOOK_ME_URL, parameters);
        }

        public static string GetMailRuUserData(string secretKey, Dictionary<string, string> parameters)
        {
            string sig = GetSig(secretKey, parameters);
            parameters.Add(MAILRU_SIG, sig);
            return GetUserData(MAILRU_ME_URL, parameters);
        }

        private static string GetSig(string secretKey, Dictionary<string, string> p)
        {
            string pp = string.Concat(p.OrderBy(x => x.Key).Select(x => string.Format("{0}={1}", x.Key, x.Value)).ToList());
            MD5 md5 = MD5.Create();
            byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(pp + secretKey));

            string sig = string.Concat(hash.Select(x => x.ToString("x2")).ToList());
            return sig;
        }

        private static string GetMD5(string key)
        {
            MD5 md5 = MD5.Create();
            byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
            return string.Concat(hash.Select(x => x.ToString("x2")).ToList());
        }
    }
}
